<template>
    <div class="login_container">
        <div class="login_wrapper">
            <el-card class="login_card">
                <div class="logo_area">
                    <img src="/logo.png" alt="Logo" class="logo_img" />
                    <h2 class="system_name">{{ system.title }}</h2>
                </div>

                <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginFormRef">
                    <h1 class="login_title">用户登录</h1>
                    <el-form-item prop="username">
                        <el-input
                            v-model="loginForm.username"
                            :prefix-icon="User"
                            placeholder="请输入用户名"
                            size="large"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input
                            v-model="loginForm.password"
                            :prefix-icon="Lock"
                            type="password"
                            placeholder="请输入密码"
                            show-password
                            size="large"
                        ></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button
                            type="primary"
                            :loading="btnLoading"
                            class="login_btn"
                            @click="login"
                            size="large"
                        >
                            登 录
                        </el-button>
                    </el-form-item>
                </el-form>

                <div class="login_footer">
                    <span>{{ footer.copyright }} - 仅供内部使用</span>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script setup lang="ts">
// 原有script部分保持不变
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { useUserStore } from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'
import { useSetting } from '@/setting'

const userStore = useUserStore()
const { system, footer } = useSetting()

const loginForm = reactive({
    username: 'admin',
    password: '111111',
})

const loginFormRef = ref()

const btnLoading = ref(false)

const $router = useRouter()
const $route = useRoute()

// 自定义表单校验规则
const validateUsername = (_rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入用户名'))
    } else {
        if (!/^[a-zA-Z0-9_]{5,10}$/.test(value)) {
            callback(new Error('用户名只能包含字母、数字和下划线，长度必须在5-10位之间'))
        } else {
            callback()
        }
    }
}

const validatePassword = (_rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入密码'))
    } else {
        if (!/^[a-zA-Z0-9_]{6,12}$/.test(value)) {
            callback(new Error('密码只能包含字母、数字和下划线，长度必须在6-12位之间'))
        } else {
            callback()
        }
    }
}

const rules = reactive({
    username: [
        {
            required: true,
            validator: validateUsername,
            trigger: 'change',
        },
    ],
    password: [
        {
            required: true,
            validator: validatePassword,
            trigger: 'change',
        },
    ],
})

const login = async () => {
    // 校验表单
    await loginFormRef.value.validate()

    btnLoading.value = true
    try {
        await userStore.userLogin(loginForm)
        let redirect: any = $route.query.redirect
        $router.push({ path: redirect || '/' })
        ElNotification({
            type: 'success',
            message: '欢迎回来',
            title: `Hi，${getTime()}好！`,
        })
        btnLoading.value = false
    } catch (error) {
        ElNotification({
            type: 'error',
            message: (error as Error).message,
        })
        btnLoading.value = false
    }
}
</script>

<style lang="scss" scoped>
.login_container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100vh;
    background: var(--el-color-primary-light-9);

    // 后期可以根据需求调整背景图片
    // background-image: url('@/assets/images/login_bg.svg');
    // background-repeat: no-repeat;
    // background-position: center;
    // background-size: cover;

    .login_wrapper {
        width: 100%;
        max-width: 420px;
        padding: 0 20px;

        .login_card {
            border: none;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgb(0 0 0 / 0.1);

            .logo_area {
                margin-bottom: 30px;
                text-align: center;

                .logo_img {
                    width: 80px;
                    height: 80px;
                    margin-bottom: 10px;
                }

                .system_name {
                    margin: 0;
                    font-size: 24px;
                    font-weight: 500;
                    color: var(--el-color-primary);
                }
            }

            .login_form {
                .login_title {
                    margin-bottom: 30px;
                    font-size: 22px;
                    font-weight: 500;
                    color: var(--el-text-color-primary);
                    text-align: center;
                }

                :deep(.el-input__wrapper) {
                    padding: 0 15px;
                }

                :deep(.el-input__inner) {
                    height: 48px;
                    line-height: 48px;
                }

                :deep(.el-input__icon) {
                    height: 48px;
                }

                .login_btn {
                    width: 100%;
                    height: 48px;
                    margin-top: 10px;
                    font-size: 16px;
                    letter-spacing: 2px;
                }
            }

            .login_footer {
                padding-top: 20px;
                margin-top: 30px;
                font-size: 12px;
                color: var(--el-text-color-secondary);
                text-align: center;
                border-top: 1px solid var(--el-border-color-light);
            }
        }
    }
}

@media (width <= 768px) {
    .login_container {
        background-image: none;

        .login_wrapper {
            padding: 0 15px;

            .login_card {
                background: transparent;
                box-shadow: none;

                .logo_area {
                    margin-bottom: 20px;

                    .logo_img {
                        width: 60px;
                        height: 60px;
                    }

                    .system_name {
                        font-size: 20px;
                    }
                }

                .login_form {
                    .login_title {
                        margin-bottom: 20px;
                        font-size: 20px;
                    }
                }
            }
        }
    }
}
</style>
